| Conditions | 1 | 
| Paths | 96 | 
| Total Lines | 170 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var formManager = function(){ | 
            ||
| 2 | /**  | 
            ||
| 3 | Code based on:  | 
            ||
| 4 | @url https://dxr.mozilla.org/firefox/source/toolkit/components/passwordmgr/src/nsLoginManager.js#655  | 
            ||
| 5 | */  | 
            ||
| 6 |     var settings = {}; | 
            ||
| 7 | |||
| 8 |     return { | 
            ||
| 9 |         _init_: function () { | 
            ||
| 10 |             API.runtime.sendMessage(API.runtime.id, {method: 'getSetting', args: 'debug'}).then(function (result) { | 
            ||
| 11 | settings.debug = (result);  | 
            ||
| 12 | });  | 
            ||
| 13 | },  | 
            ||
| 14 | /*  | 
            ||
| 15 | * _isAutoCompleteDisabled  | 
            ||
| 16 | *  | 
            ||
| 17 | * Returns true if the page requests autocomplete be disabled for the  | 
            ||
| 18 | * specified form input.  | 
            ||
| 19 | */  | 
            ||
| 20 |         isAutocompleteDisabled: function (element) { | 
            ||
| 21 |             return !!(element && element.hasAttribute("autocomplete") && element.getAttribute("autocomplete").toLowerCase() === "off"); | 
            ||
| 22 | },  | 
            ||
| 23 | |||
| 24 | /*  | 
            ||
| 25 | * _getPasswordFields  | 
            ||
| 26 | *  | 
            ||
| 27 | * Returns an array of password field elements for the specified form.  | 
            ||
| 28 | * If no pw fields are found, or if more than 3 are found, then null  | 
            ||
| 29 | * is returned.  | 
            ||
| 30 | *  | 
            ||
| 31 | * skipEmptyFields can be set to ignore password fields with no value.  | 
            ||
| 32 | */  | 
            ||
| 33 |         _getPasswordFields: function (form, skipEmptyFields) { | 
            ||
| 34 | // Locate the password fields in the form.  | 
            ||
| 35 | var pwFields = [];  | 
            ||
| 36 |             for (var i = 0; i < form.elements.length; i++) { | 
            ||
| 37 |                 if (form.elements[i].type !== "password"){ | 
            ||
| 38 | continue;  | 
            ||
| 39 | }  | 
            ||
| 40 | |||
| 41 | |||
| 42 |                 if (skipEmptyFields && !form.elements[i].value){ | 
            ||
| 43 | continue;  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 |                 pwFields[pwFields.length] = { | 
            ||
| 47 | index: i,  | 
            ||
| 48 | element: form.elements[i]  | 
            ||
| 49 | };  | 
            ||
| 50 | }  | 
            ||
| 51 | |||
| 52 | // If too few or too many fields, bail out.  | 
            ||
| 53 |             if (pwFields.length === 0) { | 
            ||
| 54 |                 this.log('(form ignored ('+ form.action +') -- no password fields.)'); | 
            ||
| 55 | return null;  | 
            ||
| 56 |             } else if (pwFields.length > 3) { | 
            ||
| 57 |                 this.log('(form ignored -- too many password fields. [got ' + | 
            ||
| 58 | pwFields.length + "])");  | 
            ||
| 59 | return null;  | 
            ||
| 60 | }  | 
            ||
| 61 | |||
| 62 | return pwFields;  | 
            ||
| 63 | },  | 
            ||
| 64 | /*  | 
            ||
| 65 | * _getFormFields  | 
            ||
| 66 | *  | 
            ||
| 67 | * Returns the username and password fields found in the form.  | 
            ||
| 68 | * Can handle complex forms by trying to figure out what the  | 
            ||
| 69 | * relevant fields are.  | 
            ||
| 70 | *  | 
            ||
| 71 | * Returns: [usernameField, newPasswordField, oldPasswordField]  | 
            ||
| 72 | *  | 
            ||
| 73 | * usernameField may be null.  | 
            ||
| 74 | * newPasswordField will always be non-null.  | 
            ||
| 75 | * oldPasswordField may be null. If null, newPasswordField is just  | 
            ||
| 76 | * "theLoginField". If not null, the form is apparently a  | 
            ||
| 77 | * change-password field, with oldPasswordField containing the password  | 
            ||
| 78 | * that is being changed.  | 
            ||
| 79 | */  | 
            ||
| 80 |         getFormFields: function (form, isSubmission) { | 
            ||
| 81 | var usernameField = null;  | 
            ||
| 82 | |||
| 83 | // Locate the password field(s) in the form. Up to 3 supported.  | 
            ||
| 84 | // If there's no password field, there's nothing for us to do.  | 
            ||
| 85 | var pwFields = this._getPasswordFields(form, isSubmission);  | 
            ||
| 86 |             if (!pwFields){ | 
            ||
| 87 | return [null, null, null];  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | |||
| 91 | // Locate the username field in the form by searching backwards  | 
            ||
| 92 | // from the first passwordfield, assume the first text field is the  | 
            ||
| 93 | // username. We might not find a username field if the user is  | 
            ||
| 94 | // already logged in to the site.  | 
            ||
| 95 |             for (var i = pwFields[0].index - 1; i >= 0; i--) { | 
            ||
| 96 |                 if (form.elements[i].type.toLowerCase() === "text" || form.elements[i].type.toLowerCase() === "email") { | 
            ||
| 97 | usernameField = form.elements[i];  | 
            ||
| 98 | break;  | 
            ||
| 99 | }  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 |             if (!usernameField){ | 
            ||
| 103 |                 this.log('(form ('+ form.action +') ignored -- no username field found)'); | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | |||
| 107 | // If we're not submitting a form (it's a page load), there are no  | 
            ||
| 108 | // password field values for us to use for identifying fields. So,  | 
            ||
| 109 | // just assume the first password field is the one to be filled in.  | 
            ||
| 110 |             if (!isSubmission || pwFields.length === 1){ | 
            ||
| 111 | var res = [usernameField, pwFields[0].element];  | 
            ||
| 112 |                 if(pwFields[1]){ | 
            ||
| 113 | res.push(pwFields[1].element);  | 
            ||
| 114 |                 } else { | 
            ||
| 115 | res.push(null);  | 
            ||
| 116 | }  | 
            ||
| 117 | return res;  | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | // Try to figure out WTF is in the form based on the password values.  | 
            ||
| 123 | var oldPasswordField, newPasswordField;  | 
            ||
| 124 | var pw1 = pwFields[0].element.value;  | 
            ||
| 125 | var pw2 = pwFields[1].element.value;  | 
            ||
| 126 | var pw3 = (pwFields[2] ? pwFields[2].element.value : null);  | 
            ||
| 127 | |||
| 128 |             if (pwFields.length === 3) { | 
            ||
| 129 | // Look for two identical passwords, that's the new password  | 
            ||
| 130 | |||
| 131 |                 if (pw1 === pw2 && pw2 === pw3) { | 
            ||
| 132 | // All 3 passwords the same? Weird! Treat as if 1 pw field.  | 
            ||
| 133 | newPasswordField = pwFields[0].element;  | 
            ||
| 134 | oldPasswordField = null;  | 
            ||
| 135 |                 } else if (pw1 === pw2) { | 
            ||
| 136 | newPasswordField = pwFields[0].element;  | 
            ||
| 137 | oldPasswordField = pwFields[2].element;  | 
            ||
| 138 |                 } else if (pw2 === pw3) { | 
            ||
| 139 | oldPasswordField = pwFields[0].element;  | 
            ||
| 140 | newPasswordField = pwFields[2].element;  | 
            ||
| 141 |                 } else if (pw1 === pw3) { | 
            ||
| 142 | // A bit odd, but could make sense with the right page layout.  | 
            ||
| 143 | newPasswordField = pwFields[0].element;  | 
            ||
| 144 | oldPasswordField = pwFields[1].element;  | 
            ||
| 145 |                 } else { | 
            ||
| 146 | // We can't tell which of the 3 passwords should be saved.  | 
            ||
| 147 |                     this.log("(form ignored -- all 3 pw fields differ)"); | 
            ||
| 148 | return [null, null, null];  | 
            ||
| 149 | }  | 
            ||
| 150 |             } else { // pwFields.length == 2 | 
            ||
| 151 |                 if (pw1 === pw2) { | 
            ||
| 152 | // Treat as if 1 pw field  | 
            ||
| 153 | newPasswordField = pwFields[0].element;  | 
            ||
| 154 | oldPasswordField = null;  | 
            ||
| 155 |                 } else { | 
            ||
| 156 | // Just assume that the 2nd password is the new password  | 
            ||
| 157 | oldPasswordField = pwFields[0].element;  | 
            ||
| 158 | newPasswordField = pwFields[1].element;  | 
            ||
| 159 | }  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | return [usernameField, newPasswordField, oldPasswordField];  | 
            ||
| 163 | },  | 
            ||
| 164 |         log: function (str) { | 
            ||
| 165 |             if(settings.debug){ | 
            ||
| 166 | console.log(str);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 167 | }  | 
            ||
| 168 | }  | 
            ||
| 169 | };  | 
            ||
| 170 | }();  | 
            ||
| 171 | |||
| 239 | formManager._init_();  |